<?php
require_once ('AppModel.php');



/**
 * @author Windows 8 Pro User
 * @version 1.0
 * @created 02-dez-2013 23:26:29
 */
class Establishment extends AppModel {

	/**
	 * Set the model name
	 */
	public $name = 'Establishment';
	/**
	 * Set the model relationship
	 */
	public $belongsTo = 'Interest';
	/**
	 * Set the model validation
	 */
	public $validate = array(
		'interest_id' => array(
			'required' => array(
				'rule' => array('notEmpty'),
				'message' => 'Id do Interesse requerido'
			)
		),
		'name' => array(
			'required' => array(
				'rule' => array('notEmpty'),
				'message' => 'Nome requerido'
			)
		)
	);
	
	/**
	 *
	 * @param options    The provided options before save
	 */
	public function beforeSave($options = array()) {
		// Upload the image
		$this->uploadImage();
	
		// Provide the return
		return true;
	}
	
	private function uploadImage() {
		// Get the absolute file path from the cache
		$pngAbsoluteFilePath = Cache::read('pngAbsoluteFilePathForEstablishmentLogo');
		
		// Clear the right cache
		Cache::delete('pngAbsoluteFilePathForEstablishmentLogo');
					
		// Check if it worked
		if ($pngAbsoluteFilePath) {					
			// Save the blob
			$this->data[$this->alias]['logo'] = base64_encode(file_get_contents($pngAbsoluteFilePath));
			
			// Delete the file
			unlink($pngAbsoluteFilePath);					
		} else {
			// Save a value
			$this->data[$this->alias]['logo'] = null;
		}			
	}

}
?>